﻿using Iworld.Author.Model;
using Iworld.Author.Service.Backstage.Export;
using Iworld.basis.Service;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using System.Threading.Tasks;

namespace Iworld.Author.Service.Backstage.Import
{
    /// <summary>
    /// 获取用户列表数据集
    /// </summary>
   [DataContract]
    public class GetUsersImport : GetPageListCheckAdministratorImportBase
    {

        /// <summary>
        /// 关键字
        /// </summary>
        [DataMember]
        public string Keyword { set; get; }

       /// <summary>
       /// 用户状态
       /// </summary>
       [DataMember]
       public AuthorStatus? Status { set; get; }

      public PageResult<AuthorExport> GetUsers(IModelToDbContextOfAuthor db)
      {
          int pageSize = 10;
          int startRow = pageSize * (this.PageIndex - 1);
          IQueryable<Model.Author> temp = db.Authors;
          int totalCount = temp.Count();
          if (Keyword != null && Keyword.Trim().Length > 0)
          {
              temp = temp.Where(x => x.Username.Contains(Keyword)
                  || x.Account.Contains(Keyword));
          }
          if(Status!=null)
          {
              temp = temp.Where(x => x.Status == this.Status);
          }
          var list = temp.OrderByDescending(x => x.CreatedTime).Skip(startRow).Take(pageSize).ToList().ConvertAll(x =>
          {
              return new AuthorExport(x);
          });
          return new PageResult<AuthorExport>(this.PageIndex, totalCount, pageSize, list);
      }

    }
}
